
package sk.lieskove.jianghongtiao.aresapi.response.obj;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.xml.xpath.XPathExpressionException;
import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import sk.lieskove.jianghongtiao.common.utils.xml.ProcessXML;

/**
 * Vypis jedné strany Přehledu FO v ARES
 * 
 * <p>Java class for vypis_stranky_fo complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType name="vypis_stranky_fo">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="S" type="{http://wwwinfo.mfcr.cz/ares/xml_doc/schemas/ares/ares_datatypes/v_1.0.4}vypis_osoby" maxOccurs="unbounded" minOccurs="0"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@Entity
@Table(name="ares_vypis_stranky_fo")
public class VypisStrankyFo implements Serializable {
    @Id
    @GeneratedValue
    private Long id;
    @OneToMany(mappedBy = "vypisStrankyFo", cascade=CascadeType.PERSIST, fetch=FetchType.EAGER)
    protected List<VypisOsoby> s;

    @Transient
    private Logger log = Logger.getLogger(VypisStrankyFo.class);
    @Transient
    private String xPath;
    @Transient
    private int index;
    @Transient
    private Document document;
    @ManyToOne
    private OdpovedFo odpovedFo;

    public VypisStrankyFo() {
    }

    VypisStrankyFo(String xPath, int index, Document document) {
        this.xPath = xPath;
        this.index=index;
        this.document = document;

        loadS();
    }

    private void loadS(){
        String xp = xPathConstructor("S");
        log.debug("Loading '"+xp+"'");
        s = new ArrayList<VypisOsoby>();
        try {
            NodeList output = ProcessXML.getNodeListByXPathQuery(xp, document);
            if (output == null) {
                log.warn("Cannot load: "+xp);
            } else {
                for (int i = 0; i < output.getLength(); i++) {
                    VypisOsoby vypisOsoby = new VypisOsoby(xp, i+1, document);
                    vypisOsoby.setVypisStrankyFo(this);
                    s.add(vypisOsoby);
                }
            }
        } catch (XPathExpressionException ex) {
            log.warn("Cannot load '"+xp+"' property. ",ex);
        }
    }

    private String xPathConstructor(String subNode){
        return xPath + "[" + index + "]/" + subNode;
    }

    /**
     * Gets the value of the s property.
     * 
     * <p>
     * This accessor method returns a reference to the live list,
     * not a snapshot. Therefore any modification you make to the
     * returned list will be present inside the JAXB object.
     * This is why there is not a <CODE>set</CODE> method for the s property.
     * 
     * <p>
     * For example, to add a new item, do as follows:
     * <pre>
     *    getS().add(newItem);
     * </pre>
     * 
     * 
     * <p>
     * Objects of the following type(s) are allowed in the list
     * {@link VypisOsoby }
     * 
     * 
     */
    public List<VypisOsoby> getS() {
        if (s == null) {
            s = new ArrayList<VypisOsoby>();
        }
        return this.s;
    }

    public void setS(List<VypisOsoby> s) {
        this.s = s;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public OdpovedFo getOdpovedFo() {
        return odpovedFo;
    }

    public void setOdpovedFo(OdpovedFo odpovedFo) {
        this.odpovedFo = odpovedFo;
    }

}
